package Datos;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.CallableStatement;
import java.util.*;

public class Cargo {
	Sesion oSesion;
	
	public Cargo() {};
	
	public int Add(Entidades.Cargo item) throws Exception{
		try{
		Connection oCnn = Datos.General.CrearConexion();
		String storedProcedure = "{call cpd.CargoAdd(?,?)}";
		CallableStatement cs =oCnn.prepareCall(storedProcedure);
		cs.setInt("mIdCargo", item.getIdCargo());
		cs.setString("mDscCargo", item.getDscCargo());

		boolean pipi = cs.execute();
		System.out.println("Resultado booleano: " + pipi);
		int numero = 0;
		if (pipi) numero = 1;
		return numero;
		
		} catch (Exception ex){
			   System.out.println("SQLException: " + ex.getMessage());
			   return -1;
		}
	}
	public void Update(Entidades.Cargo item) throws Exception{
		try{
			Connection oCnn = Datos.General.CrearConexion();
			String storedProcedure = "{call cpd.CargoUpdate(?,?)}";
			CallableStatement cs =oCnn.prepareCall(storedProcedure);
			cs.setInt("mIdCargo", item.getIdCargo());
			cs.setString("mDscCargo", item.getDscCargo());

			boolean pipi = cs.execute();
			System.out.println("Resultado booleano: " + pipi);
			return;
		} catch (Exception ex){
			   System.out.println("SQLException: " + ex.getMessage());
		}
	}
	public void Delete(int id) throws Exception{
		try{
			Connection oCnn = Datos.General.CrearConexion();
			String storedProcedure = "{call cpd.CargoDelete(?)}";
			CallableStatement cs =oCnn.prepareCall(storedProcedure);
			cs.setInt("mIdCargo", id);

			boolean pipi = cs.execute();
			System.out.println("Resultado booleano: " + pipi);
			return;
		} catch (Exception ex){
			   System.out.println("SQLException: " + ex.getMessage());
		}
	}
	public List GetOne(int id) throws Exception{
		List oDatos = new ArrayList();
		Statement stmt = null;   //
		ResultSet rs = null;	//
		Connection oCnn = Datos.General.CrearConexion();
		String storedProcedure = "{call cpd.CargoGetOne(?)}";
		CallableStatement cs = oCnn.prepareCall(storedProcedure);
		cs.setInt("mIdCargo", id);
		rs = cs.executeQuery();
		return sacaDatos(oDatos, rs);
	}

	public List GetAll() throws Exception{
		List oDatos = new ArrayList();
		Statement stmt = null;   //
		ResultSet rs = null;	//
		Connection oCnn = Datos.General.CrearConexion();
		String storedProcedure = "{call cpd.CargoGetAll()}";
		CallableStatement cs = oCnn.prepareCall(storedProcedure);
		rs = cs.executeQuery();
		return sacaDatos(oDatos, rs);
	}
	public List Primero() throws Exception{
		return Trae("FIRST");
	}
	public List Siguiente() throws Exception{
		return Trae("NEXT");
	}
	public List Anterior() throws Exception{
		return Trae("PREV");
	}
	public List Ultimo() throws Exception{
		return Trae("LAST");
	}
	public void Abrir() throws Exception{
		oSesion = new Sesion();
		try{
			String sql = "HANDLER `cargo` OPEN";
			oSesion.getSesion().executeQuery(sql);
		} catch (Exception ex){
			System.out.println("SQLException: " + ex.getMessage());
		}
	}
	public void Cerrar() throws Exception{
		try{
			String sql = "HANDLER `cargo` CLOSE";
			oSesion.getSesion().executeQuery(sql);
		} catch (Exception ex){
			System.out.println("SQLException: " + ex.getMessage());
		}
	}
	public List Trae(String condicion) throws Exception{
		List oDatos = new ArrayList();
		ResultSet rs = null;	//
		String sql = "HANDLER `cargo` READ `PRIMARY` " + condicion + "";
		rs = oSesion.getSesion().executeQuery(sql);  //
		return sacaDatos(oDatos, rs);
	}
	private List sacaDatos(List oDatos, ResultSet rs){
		try {
			while(rs.next()){
				Entidades.Cargo oDato = new Entidades.Cargo(rs.getInt(1), rs.getString(2));
				oDatos.add(oDato);
				oDato = null;
			}
			rs.next();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return oDatos;
	}
}
